﻿using AutoMapper;
using HealthcareSystem.API.Read.Application.Command.ChronicDisease;
using HealthcareSystem.API.Read.DTO.ChronicDTO;
using HealthcareSystem.Domain;
using HealthcareSystem.Domain.ChronicDiseaseManagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Read.Application.CommandHandel.ChronicDisease
{
    public class ChronicDataShowCommandHandler : IRequestHandler<ChronicDataShowCommand, ApiResult<ApiPaging<List<ChronicDataDTO>>>>
    {

        private readonly IBaseRepository<ChronicList> chRepository;
        private readonly IBaseRepository<Record> reRepository;
        private readonly IBaseRepository<ChronicSign> csRepository;
        private readonly IBaseRepository<Physique> phRepository;
        private readonly IMapper mapper;

        public ChronicDataShowCommandHandler(IBaseRepository<ChronicList> chRepository, IBaseRepository<Record> reRepository, IBaseRepository<ChronicSign> csRepository, IBaseRepository<Physique> phRepository, IMapper mapper)
        {
            this.chRepository = chRepository;
            this.reRepository = reRepository;
            this.csRepository = csRepository;
            this.phRepository = phRepository;
            this.mapper = mapper;
        }

        public Task<ApiResult<ApiPaging<List<ChronicDataDTO>>>> Handle(ChronicDataShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<ChronicDataDTO>>> result = new ApiResult<ApiPaging<List<ChronicDataDTO>>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();

            var list = from a in chRepository.GetAll()
                       join b in reRepository.GetAll() on a.RecordId equals b.RecordId
                       join c in csRepository.GetAll() on a.CSignId equals c.CSignId
                       join d in phRepository.GetAll() on a.PhysiqueId equals d.PhysiqueId
                       select new ChronicDataDTO
                       {
                           ChronicId = a.ChronicId,
                           RecordId = b.RecordId,
                           RecordName = b.RecordName,
                           RecordSex = b.RecordSex,
                           RecordAge = b.RecordAge,
                           RecordIdcard = b.RecordIdcard,
                           RecordPhone = b.RecordPhone,
                           RecordSite = b.RecordSite,
                           RecordTeam = b.RecordTeam,
                           RecordState = b.RecordState,
                           RecordDate = b.RecordDate,
                           RecordMarriage = b.RecordMarriage,
                           RecordBlood = b.RecordBlood,
                           RecordInsured = b.RecordInsured,
                           RecordProfession = b.RecordProfession,
                           RecordCulture = b.RecordCulture,
                           CSignId = c.CSignId,
                           CSignTeam = c.CSignTeam,
                           CSignBegin = c.CSignBegin,
                           CSignEnd = c.CSignEnd,
                           CSignManage = c.CSignManage,
                           CSignLevel = c.CSignLevel,
                           CSignImage = c.CSignImage,
                           CSignIsdel = c.CSignIsdel,
                           PhysiqueId = d.PhysiqueId,
                           PhysiqueHeight = d.PhysiqueHeight,
                           PhysiqueWeight = d.PhysiqueWeight,
                           PhysiqueBMI = d.PhysiqueBMI,
                           PhysiqueWaistline = d.PhysiqueWaistline,
                           PhysiqueShrink = d.PhysiqueShrink,
                           PhysiqueDiastole = d.PhysiqueDiastole,
                           PhysiqueFBG = d.PhysiqueFBG
                       };
            if (!string.IsNullOrEmpty(request.RecordName))
            {
                list = list.Where(x => x.RecordName.Contains(request.RecordName));
            }
            var totalCount = list.Count();
            var totalPage = (int)Math.Ceiling((totalCount * 1.0) / request.PageSize);
            list = list.OrderBy(x => x.ChronicId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            ApiPaging<List<ChronicDataDTO>> paging = new ApiPaging<List<ChronicDataDTO>>();
            paging.totalPage = totalPage;
            paging.totalCount = totalCount;
            paging.Data = list.ToList();
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
